﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace GUI
{
    // This declaration defines a delegate named DataGridDisableCellHandler,
    // which will encapsulate any method that takes two parameters:
    // object: the source of the event; that is the publishing object
    // DataGridDisableCellEventArgs: an object derived from EventArgs.
    public delegate void DataGridDisableCellHandler(object sender, DataGridDisableCellEventArgs e);

    // ====================== Our Publisher ================================= 
    // This Class is our second Paramter for our Event, it must derived
    // from EventArgs. We publish the Properties: Column, Row and EnableValue
    // to our Subscribers.
    public class DataGridDisableCellEventArgs : EventArgs
    {
        private int _column;
        private int _row;
        private bool _enablevalue = true;

        // Constructor, set private values row, col.
        public DataGridDisableCellEventArgs(int row, int col)
        {
            _row = row;
            _column = col;
        }

        // Published Property Column
        public int Column
        {
            get { return _column; }
            set { _column = value; }
        }

        // Published Property Row
        public int Row
        {
            get { return _row; }
            set { _row = value; }
        }

        // Published Property EnableValue
        public bool EnableValue
        {
            get { return _enablevalue; }
            set { _enablevalue = value; }
        }
    }

    // This is our own, customized DataGrid TextBox Column. Here
    // we draw the Cell Background and disable / enable the Cell
    // according to the EnableValue in the defined Event.
    public class DataGridTextBox : DataGridTextBoxColumn
    {
        // Declare the Event for the defined Delegate.
        public event DataGridDisableCellHandler DataGridDisableCell;

        // Save the column number
        private int _col;

        // Our own Constructor, which must NOT conform the Constructor
        // in the Base Class (Constructors are not derived)
        public DataGridTextBox(int column)
        {
            _col = column;
        }

        // Here is the trick to emable / disable the TextBox
        // of the Cell - override the Edit method, with our
        // own functionality.
        protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        {
            DataGridDisableCellEventArgs e = null;

            // Do we have Subscribers - notify them if we have
            if (DataGridDisableCell != null)
            {
                // Initialize our Event with the current Row and Column Number
                e = new DataGridDisableCellEventArgs(rowNum, _col);

                // Notify Subscribers to call their EventHandlers - where they
                // can do whatever they want. After this we check the EnableValue
                // Flag, which may be set / unset by a Subscriber.			
                DataGridDisableCell(this, e);
            }
        }
    }
}
